package com.cuitrip.business.setting.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.cuitrip.business.home.IndexActivity;
import com.cuitrip.business.home.trip.TripServiceListFragment;
import com.cuitrip.business.login.LoginInstance;
import com.cuitrip.business.setting.SubmitSuccessActivity;
import com.cuitrip.business.setting.model.SubmitSuccessModel;
import com.cuitrip.business.tripservice.CreateTripEditMainFragment;
import com.cuitrip.business.tripservice.model.ServiceInfo;
import com.cuitrip.business.user.CompleteBasicUserInfoActivity;
import com.cuitrip.business.user.VerifyMobileSuccessFragment;
import com.cuitrip.business.user.model.CtUserInfo;
import com.cuitrip.service.R;
import com.lab.rtenvironment.AppCoreRuntime;

import java.lang.ref.WeakReference;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by ak
 */
public class SuccessViewHolder {

    @Bind(R.id.main_title)
    TextView mainTitle;
    @Bind(R.id.content)
    TextView mContent;
    @Bind(R.id.return_action)
    TextView mReturnAction;
    private WeakReference<SubmitSuccessActivity> activityReference;
    private Bundle mBundle;

    public void setDataExtras(Bundle bundle){
        this.mBundle=bundle;
    }

    public int withLayoutRes() {
        return R.layout.ct_commit_feedback_page;
    }

    public void build(SubmitSuccessActivity activity) {
        this.activityReference = new WeakReference<SubmitSuccessActivity>(activity);
        ButterKnife.bind(this, activity);
        boolean isTraveller = LoginInstance.getInstance().getUserInfo().isTravel();
        if (isTraveller) {
            mReturnAction.setBackgroundResource(R.drawable.selector_orange_rec_corner);
        } else {
            mReturnAction.setBackgroundResource(R.drawable.selector_blue_rec_corner);
        }
    }

    public void render(SubmitSuccessModel submitSuccessModel) {
        if (!TextUtils.isEmpty(submitSuccessModel.getTitle())) {
            mainTitle.setVisibility(View.VISIBLE);
            mainTitle.setText(submitSuccessModel.getTitle());
        } else {
            mainTitle.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(submitSuccessModel.getContent())) {
            mContent.setVisibility(View.VISIBLE);
            mContent.setText(submitSuccessModel.getContent());
        } else {
            mContent.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(submitSuccessModel.getActionText())) {
            mReturnAction.setVisibility(View.VISIBLE);
            mReturnAction.setText(submitSuccessModel.getActionText());
        } else {
            mReturnAction.setVisibility(View.GONE);
        }

    }

    public void doAction(SubmitSuccessModel submitSuccessModel) {
        if (getActivity() == null) {
            return;
        }
        boolean isTraveller = LoginInstance.getInstance().getUserInfo().isTravel();
        switch (submitSuccessModel.getType()) {
            case CREATE_TRIP:
                IndexActivity.gotoTab(getActivity(), IndexActivity.SERVICE_TAB_INDEX);
                getActivity().finish();
                break;
            case FEEDBACK_SUCCESS:
                getActivity().finish();
                break;
            case CANCEL_ORDER:
                if (isTraveller) {
                    IndexActivity.gotoTab(getActivity(), IndexActivity.RECOMMEND_TAB_INDEX);
                } else {
                    IndexActivity.gotoTab(getActivity(), IndexActivity.ORDER_TAB_INDEX);
                }
                break;
            case VERIFIED_SUCCESS:
                if (!TextUtils.isEmpty(mContent.getText())) {
                    getActivity().setResult(Activity.RESULT_OK);
                    getActivity().finish();
                }
                break;

            case COMPLETE_INSIDER_INFO:
                if (mBundle!=null){
                    ServiceInfo serviceInfo= (ServiceInfo) mBundle.getSerializable(ServiceInfo.KEY);
                    CtUserInfo userInfo= (CtUserInfo) mBundle.getSerializable(CtUserInfo.KEY);
                    int type = mBundle.getInt("type");
                    CompleteBasicUserInfoActivity.startActivity(getActivity(), serviceInfo, userInfo, type);
                }
                getActivity().finish();
                break;

        }
    }

    private SubmitSuccessActivity getActivity() {
        if (activityReference != null && activityReference.get() != null) {
            return activityReference.get();
        } else {
            return null;
        }
    }


}
